package net.stogi.unicore.beyond.ui.client.taskeditor.iotoolbox;

import java.util.HashMap;
import java.util.Map;

import net.stogi.common.mvp.core.behavior.Behavior;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;

class IoToolboxWidget extends Composite implements IoToolboxView {

	private final Button inputGroupButton = new Button();

	private final Map<String, Button> inputTypeMap = new HashMap<String, Button>();
	
	private final ContentPanel mainPanel = new ContentPanel();
	
	private final Button outputGroupButton = new Button();
	
	private final Map<String, Button> outputTypeMap = new HashMap<String, Button>();
	
	public IoToolboxWidget() {
		initComposite();
		initI18n();
		initLayout();
		initComponent(mainPanel);
	}

	@Override
	public void addInputType(String name, Behavior behavior) {
		inputTypeMap.put(name, doCreateIoTypeButton(name, behavior));
	}

	@Override
	public void addOutputType(String name, Behavior behavior) {
		outputTypeMap.put(name, doCreateIoTypeButton(name, behavior));
	}

	@Override
	public void blockInputTypes() {
		doSetIoTypesEnabled(inputTypeMap, false);
	}

	@Override
	public void blockOutputTypes() {
		doSetIoTypesEnabled(outputTypeMap, false);
	}

	@Override
	public void setInputGroupBehavior(final Behavior behavior) {
		inputGroupButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setOutputGroupBehavior(final Behavior behavior) {
		outputGroupButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}
	
	@Override
	public void unblockInputTypes() {
		doSetIoTypesEnabled(inputTypeMap, true);
	}

	@Override
	public void unblockOutputTypes() {
		doSetIoTypesEnabled(outputTypeMap, true);
	}
	
	private Button doCreateIoTypeButton(String name, final Behavior behavior) {
		final Button ioTypeButton = new Button(name);
		ioTypeButton.setWidth("100%");
		ioTypeButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
		mainPanel.add(ioTypeButton);
		mainPanel.layout();
		return ioTypeButton;
	}

	private void doSetIoTypesEnabled(Map<String, Button> ioTypeMap, boolean enabled) {
		for (Button inputType : ioTypeMap.values()) {
			inputType.setEnabled(enabled);
		}
	}

	private void initComposite() {
		// mainPanel.setBodyStyle("background: white;");
		// mainPanel.setBodyBorder(true);
		mainPanel.setFrame(true);
		inputGroupButton.setWidth("100%");
		outputGroupButton.setWidth("100%");
	}

	private void initI18n() {
		mainPanel.setHeading("Input / Output Toolbox");
		inputGroupButton.setText("Input Group");
		outputGroupButton.setText("Output Group");
	}

	private void initLayout() {
		mainPanel.add(inputGroupButton);
		mainPanel.add(outputGroupButton);
	}
}
